<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/7/23
 * Time: 17:50
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\GroupToursLine as GT;
use think\Cache;
use think\Request;

class GroupToursLine extends Api{
    public function __construct()
    {
        parent::__construct();
    }

    public function checAuth($post)
    {
        //验证鉴权
        $match_arr = array(
            "secret",
            "secret_code"
        );
        $match_res = $this->match_post($match_arr,$post);
        if($match_res['code']==200){
            if(!$this->ApiValidate($post['secret_code'],$post['secret'])){
                //鉴权失败
                return false;
            }else{
                return true;
            }
        }

        return $match_res;
    }
    //获取拼团路线列表
    public function getGroupToursList()
    {
        $post = request()->post();
        //鉴权
        $auth_res = $this->checAuth($post);
        if(is_array($auth_res)){
            return json_encode($auth_res);
        }elseif(!$auth_res){
            return json_encode(['code'=>400,'msg'=>'鉴权失败']);
        }
        $city_id = $post['city_id']??0;
        $page = $post['page']??1;
        $offset = $post['offset']??10;

        $model = new GT();
        list($total,$list) = $model->getList($city_id,$page,$offset);
        $data['total'] = $total;
        $data['totalPage'] = ceil($total/$offset);
        $data['curPage'] = (int)$page;
        $data['offset'] = (int)$offset;
        $data['list'] = $list;
        return json_encode(['code'=>200,'msg'=>'success','data'=>$data]);
    }

    public function getGroupToursInfo()
    {
        $post = request()->post();
        //鉴权
        $auth_res = $this->checAuth($post);
        if(is_array($auth_res)){
            return json_encode($auth_res);
        }elseif(!$auth_res){
            return json_encode(['code'=>400,'msg'=>'鉴权失败']);
        }

        $line_id = $post['line_id']??0;
        $model = new GT();

        $detail = $model->getDetail($line_id,'orderSystem',true);
        if(empty($detail)){
            return json_encode(['code'=>404,'msg'=>'data is empty']);
        }else{
            return json_encode(['code'=>200,'msg'=>'success','data'=>$detail]);
        }
    }
    public function getGroupSpecPrice()
    {
        $post = request()->post();
        //鉴权
        $auth_res = $this->checAuth($post);
        if(is_array($auth_res)){
            return json_encode($auth_res);
        }elseif(!$auth_res){
            return json_encode(['code'=>400,'msg'=>'鉴权失败']);
        }
        $date = $post['date']??"";
        $line_id = $post['line_id']??0;
        $line = db("group_tours_line")->find($line_id);
        if(empty($line)){
            return json_encode(['code'=>-1,'msg'=>'线路信息不存在']);
        }
        $model = new GT();
        $priceList = $model->getGoodsSpecPriceByDate($line['product_id'],$date);
        if(empty($priceList)){
            return json_encode(['code'=>-1,'msg'=>'套餐暂未配置']);
        }else{
            return json_encode(['code'=>200,'msg'=>'success','data'=>$priceList]);
        }
    }

    /**
     * 获取酒店房价列表
     */
    public function getGroupRoomPrice()
    {
        $model = new GT();
        $line_id = 1030;
        $list = $model->getRoomPrice($line_id);
        return json_encode(['status'=>200,'msg'=>'success','data'=>$list]);exit;
    }

}